package org.bonorsoft.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.constant.CacheConstant;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.util.StringUtils;
import org.bonorsoft.modules.system.entity.SysDict;
import org.bonorsoft.modules.system.entity.SysDictItem;
import org.bonorsoft.modules.system.mapper.SysDictMapper;
import org.bonorsoft.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 字典类别
 * @Author: WilliamYe
 * @Date:   2020-11-18
 * @Version: V1.0
 */
@Service
@Slf4j
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;


	/**
	 * 通过查询指定code 获取字典值text
	 * @param code
	 * @param key
	 * @return
	 */

	@Override
	@Cacheable(value = CacheConstant.SYS_DICT_CACHE,key = "#code+':'+#key")
	public String queryDictTextByKey(String code, String key) {
		log.info("无缓存dictText的时候调用这里！");
		return sysDictMapper.queryDictTextByKey(code, key);
	}



	/**
	 * 通过查询指定table的 text code 获取字典值text
	 * dictTableCache采用redis缓存有效期10分钟
	 * @param table
	 * @param text
	 * @param code
	 * @param key
	 * @return
	 */
	@Override
	@Cacheable(value = CacheConstant.SYS_DICT_TABLE_CACHE)
	public String queryTableDictTextByKey(String table,String text,String code, String key) {
		log.info("无缓存dictTable的时候调用这里！");
		return sysDictMapper.queryTableDictTextByKey(table,text,code,key);
	}

	/**
	 * 校验字典类型称是否唯一
	 *
	 * @param dict 字典类型
	 * @return 结果
	 */
	@Override
	public String checkDictTypeUnique(SysDict dict)
	{
		String dictId = StringUtils.isNull(dict.getId()) ? "-1" : dict.getId();
		QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dict_code", dict.getDictCode());
		SysDict dictType = this.getOne(queryWrapper);
		if (StringUtils.isNotNull(dictType) && !dictType.getId().equals(dictId))
		{
			return UserConstants.DICT_TYPE_NOT_UNIQUE;
		}
		return UserConstants.DICT_TYPE_UNIQUE;
	}

	@Override
	public String getDictCode(String dictId) {
		QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", dictId);
		return this.getOne(queryWrapper).getDictCode();
	}

	/**
	 * 清空缓存数据
	 */
	@Override
	@Caching(evict={@CacheEvict(value = CacheConstant.SYS_DICT_CACHE_TOTAL, allEntries=true),
			@CacheEvict(value = CacheConstant.SYS_DICT_CACHE_ITME,allEntries=true),
			@CacheEvict(value = CacheConstant.SYS_DICT_CACHE,allEntries=true),
			@CacheEvict(value = CacheConstant.SYS_DICT_TABLE_CACHE,allEntries=true)})
	public void clearCache()
	{
	}

	/**
	 * 获取永安字典值
	 * @param table
	 * @param text
	 * @param code
	 * @param key
	 * @param param
	 * @return
	 */
	@Override
	@Cacheable(value = CacheConstant.SYS_YONG_TABLE_CACHE)
	public String queryValueByParam(String table, String text, String code, String key, String param) {
		log.info("永安系统-无缓存dictTable的时候调用这里！");
		return sysDictMapper.queryValueByParam(table,text,code,key,param);
	}

}
